<!--
* coder  : dzlua
* email  : 505544956@qq.com
* module : ApiMgr-client
* path   : src/views
* file   : login.vue
* time   : 2017-11-14 15:52:42
-->
<template>
  <div class="login">
    <am-header></am-header>
    <el-form class="login-container" v-bind:rules="rules" ref="ruleForm" v-bind:model="ruleForm">
      <el-form-item label="账号/邮箱/手机号" prop="username">
        <el-input type="text" :autofocus="true" auto-complete="off" v-model="ruleForm.username" placeholder="请输入账号/邮箱/手机号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input @keyup.enter.native="handleSubmit" type="password" auto-complete="off" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" v-on:click="handleSubmit">登录</el-button>
        <el-button v-on:click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <am-footer></am-footer>
  </div>
</template>

<script>
  import api from '../api/index'
  import auth from '../auth'
  export default {
    name: 'login',
    beforeRouteEnter (to, from, next) {
      let refreshToken = auth.refreshToken()
      if (refreshToken) {
        api.req({
          url: 'login',
          method: 'POST',
          token: refreshToken
        }, resp => {
          if (!resp.status.success) {
            next()
            return
          }
          auth.saveLogin(resp.data)
          next({ name: 'home' })
        })
      } else {
        next()
      }
    },
    data () {
      // 姓名验证
      let validatorName = function (rule, value, callback) {
        if (!value) {
          callback(new Error('请输入账号/邮箱/手机号'))
        } else if (!/^[A-Za-z0-9_\-\u4e00-\u9fa5]{3,16}$/.test(value) &&
            !/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value) &&
            !/^1\d{10}$/.test(value)) {
          callback(new Error('账号/邮箱/手机号 格式不正确'))
        } else {
          callback()
        }
      }
      // 密码验证
      let validatorPass = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入密码'))
        } else if (value.length > 16 || value.length < 6) {
          callback(new Error('密码只能是6-16位'))
        } else {
          callback()
        }
      }
      return {
        // 表单数据
        ruleForm: {
          username: '',
          password: ''
        },
        // 验证规则
        rules: {
          username: [
            { validator: validatorName, trigger: 'blur' }
          ],
          password: [
            { validator: validatorPass, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 表单提交
      handleSubmit () {
        // 验证表单
        this.$refs.ruleForm.validate(valid => {
          if (!valid) return
          this.$api.req({
            url: 'login',
            method: 'POST',
            username: this.ruleForm.username,
            password: this.ruleForm.password
          }, resp => {
            if (!resp.status.success) {
              this.$message.error(JSON.stringify(resp.status.message))
              return
            }
            this.$auth.saveLogin(resp.data)
            this.$message.success(resp.status.message)
            this.$router.push({ name: 'home' })
          })
        })
      },
      // 重置表单
      handleReset () {
        this.$refs.ruleForm.resetFields()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .login-container {
    /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
    .title {
      margin: 0px auto 40px auto;
      text-align: center;
      color: #505458;
    }
    .remember {
      margin: 0px 0px 35px 0px;
    }
  }
</style>
